package com.ahu.controller;

import com.ahu.Repository.RepairCurRepository;
import com.ahu.Repository.RepairInfoRepository;
import com.ahu.Repository.RepairLogRepository;
import com.ahu.Repository.UserInfoRepository;
import com.ahu.domain.RepairCur;
import com.ahu.domain.RepairLog;
import com.ahu.domain.RepairTransfer;
import com.ahu.util.Response;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.NoResultException;

/**
 * Created by qx on 2017/11/30.
 */
@RestController
@RequestMapping("/v1")

public class RepairController {
    private final RepairCurRepository repairCurRepository;
    private final RepairLogRepository repairLogRepository;
    private final UserInfoRepository userInfoRepository;
    private final RepairInfoRepository repairInfoRepository;
//    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    public RepairController(RepairCurRepository repairCurRepository,
                            RepairLogRepository repairLogRepository,
                            UserInfoRepository userInfoRepository,
                            RepairInfoRepository repairInfoRepository) {
        this.repairCurRepository = repairCurRepository;
        this.repairLogRepository = repairLogRepository;
        this.userInfoRepository = userInfoRepository;
        this.repairInfoRepository = repairInfoRepository;
    }

//    @RequestMapping(value = "/repair", method = RequestMethod.GET)
//    public Response repairCur(String repairAddress,String repairContext){
//        RepairCur repairCur = new RepairCur();
//
//        repairCur.setRepairAddress(repairAddress);
//        repairCur.setRepairContext(repairContext);
//        repairCurRepository.save(repairCur);
//        return new Response().success(repairCur);
//
//    }


    @ApiOperation(value = "报修信息查询")
    @RequestMapping(value = "/inquireinfo", method = RequestMethod.GET)
    public Response repairInfo(String number){

        return new Response().success(repairInfoRepository.findByNumber(number));
    }

    @Transactional
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public Response Update(String repairStatus, String number) {
        try {
//            RepairCur repairCur = repairCurRepository.findByNumber(number);
            repairCurRepository.updateStatusByNumber(repairStatus, number);
            return new Response().success();

        }catch (NoResultException e){
            throw e;
        }

    }

    @Transactional
    @RequestMapping(value = "/cancel", method = RequestMethod.GET)
    public Response Cancel(Integer id, String repairOrderNumber){
        RepairCur repairCur = repairCurRepository.findByIdAndRepairOrderNumber(id, repairOrderNumber);
        repairCur.setRepairStatus("已取消");

        repairLogRepository.save(RepairTransfer.RepairTransfer(repairCur));

        repairCurRepository.delete(repairCur);
        return new Response().success();
    }


}
